//
//  CoinTransViewController.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/5/5.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class CoinTransViewController: BaseViewController, SelectmangerDelegate {
    var viewModel:CoinTransViewModel?
    lazy var coinTransView:CoinTransView = {
        let view = CoinTransView(dataSource: viewModel)
        
        return view
    }()
    lazy var receiveBtn:UIButton = {
        let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
        btn.addTarget(self, action: #selector(receiveBtnAction), for: UIControlEvents.touchUpInside)
        btn.setImage(UIImage(named: "assetReceive"), for: UIControlState.normal)
        return btn
    }()
    lazy var sendBtn:UIButton = {
        let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
        btn.setImage(UIImage(named: "assetSend"), for: UIControlState.normal)
        btn.addTarget(self, action: #selector(sendAction), for: UIControlEvents.touchUpInside)
        return btn
    }()
    
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        viewModel = CoinTransViewModel(controller: self)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.title = viewModel?.model.coinCode
        self.setBlackNavigationBar()
        self.hiddenNavgationBarLine(hidden: true)
        viewModel?.archiveMoney()
        coinTransView.setModel()
        view.addSubview(coinTransView)
        coinTransView.snp.makeConstraints { (make) in
            make.top.left.bottom.right.equalToSuperview()
        }
        
        APPAssetInfo.default.archiveAll(self.view, block: {() in
            self.viewModel?.checkCurrentModel()
            self.coinTransView.setModel()
            self.viewModel?.archiveMoney()
        })
        
        viewModel?.archiveInformation()
        
        if viewModel?.type == .coinType {
            
        let item  = UIBarButtonItem(customView: receiveBtn)
        let sendItem = UIBarButtonItem(customView: sendBtn)
        self.navigationItem.rightBarButtonItems = [sendItem,item]
            
            
        }
        self.navigationItem.rightBarButtonItem?.tintColor = UIColor.white
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.convention()
        viewModel?.openTimer()
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        UIApplication.shared.statusBarStyle = .default
        viewModel?.closeTimer()
    }
    
    @objc func gotoLandscape() {
        self.navigationController?.pushViewController(AssetDetailViewController(), animated: true)
    }
    
   //    SelectmangerDelegate
    func selectModel(model: SheetModel) {
        
    }
    
    @objc func sendAction() {
        if !AppSecurityInfo.default.model.isTradePwd {
            CYMBShow.showMessage(kText("请先设置交易密码"))
            return
        }
        let controller = SendCoinViewController()
        viewModel?.archiveModelIndex()
        controller.viewModel?.selectIndex = (viewModel?.selectIndex)!
        self.navigationController?.pushViewController(controller, animated: true)
    }
    @objc func receiveBtnAction() {
        if APPAssetInfo.default.coninKidArray.count == 0 {
            CYMBShow.showMessage(kText("当前币种无法充值"))
            return
        }
        
        
        let controller = ReceiveCoinViewController()
        for model in APPAssetInfo.default.coninKidArray {
            if model.coinCode == viewModel?.model.coinCode {
                controller.viewModel?.selectModel  = model
                break
            }
        }
        if controller.viewModel?.selectModel.isrecharge == nil {
            CYMBShow.showMessage(kText("当前币种无法充值"))
            return
        }
        if controller.viewModel?.selectModel.isrecharge == 0 {
            CYMBShow.showMessage(kText("当前币种无法充值"))
            return
        }
        var canUseArr:[TradeCoinModel] = []
        for model  in APPAssetInfo.default.coninKidArray {
            let useNumber = model.isrecharge ?? 0
            if useNumber != 0 {
                canUseArr.append(model)
            }
        }
        controller.viewModel?.coninKidArray = canUseArr
        self.viewModel?.controller?.navigationController?.pushViewController(controller, animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
